﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//json数据获取
namespace MachineBuildingSystem
{
    public class DataHelper
    {
        //获取订单
        public static List<OrderInfo> GetSaleOrders(string user, string password, int belongcorpId, long orderId, string orderCode, long customerId, long materialId,
            DateTime start, DateTime end, string types, string audites, int pageNo, int pageSize)
        {
            string jsondata = "";
            List<OrderInfo> saleOrdersList = new List<OrderInfo>();
            try
            {
                string parameterStr = orderId + "," + belongcorpId + "," + orderCode + "," + customerId + ",,," + materialId + ",,,," + (start == DateTime.MinValue ? null : start.ToString("yyyy-MM-dd"))
                    + "," + (end == DateTime.MaxValue ? null : end.ToString("yyyy-MM-dd")) + "," + types + "," + audites + "," + pageNo + "," + pageSize;
            
                string strErrMsg = string.Empty;
                int res = CommonHelper.getJsonData("saleOrders", parameterStr, ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password,ref name))
                            return null;
                        return GetSaleOrders(user, password, belongcorpId, orderId, orderCode, customerId, materialId, start, end, types, audites, pageNo, pageSize);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
                else//json取值正常
                {
                    jsondata = CGlobal.strSwapJsonData;
                    CGlobal.strSwapJsonData = string.Empty;
                    if (jsondata == "[]")
                        return null;
                    JArray jar = JArray.Parse(jsondata);
                    for (int i = 0; i < jar.Count; i++)
                    {
                        OrderInfo order = new OrderInfo();
                        order.mainId = long.Parse(jar[i]["mainId"].ToString());
                        order.id = long.Parse(jar[i]["id"].ToString());
                        order.orderCode = jar[i]["orderCode"].ToString().Trim();
                        order.orderDate = Convert.ToDateTime(jar[i]["orderDate"].ToString());
                        if (jar[i]["corpName"] != null && !string.IsNullOrEmpty(jar[i]["corpName"].ToString()))
                            order.corpName = jar[i]["corpName"].ToString();
                        order.corpId = belongcorpId;
                        order.customerId = long.Parse(jar[i]["customerId"].ToString());
                        order.customerCode = jar[i]["customerCode"].ToString().Trim();
                        order.customerName = jar[i]["customerName"].ToString().Trim();
                        order.materialId = long.Parse(jar[i]["materialId"].ToString());
                        order.materialCode = jar[i]["materialCode"].ToString().Trim();
                        order.materialName = jar[i]["materialName"].ToString().Trim();
                        order.materialSstd = jar[i]["materialSstd"].ToString().Trim();
                        //物料坍落度
                        if (!string.IsNullOrEmpty(jar[i]["slump1"].ToString().Trim()))
                            order.materialSlump = jar[i]["slump1"].ToString().Trim();
                        if (!string.IsNullOrEmpty(jar[i]["orderQuantity"].ToString()))
                            order.orderQuantity = decimal.Parse(jar[i]["orderQuantity"].ToString());
                        else
                            order.orderQuantity = 0;
                        if (!string.IsNullOrEmpty(jar[i]["sendQuantity"].ToString()))
                            order.sendQuantity = decimal.Parse(jar[i]["sendQuantity"].ToString());
                        else
                            order.sendQuantity = 0;
                        if (!string.IsNullOrEmpty(jar[i]["unitPrice"].ToString()))
                            order.productPrice = decimal.Parse(jar[i]["unitPrice"].ToString());
                        else
                            order.productPrice = 0;
                        if (jar[i]["phone"] != null && !string.IsNullOrEmpty(jar[i]["phone"].ToString()))
                            order.phone = jar[i]["phone"].ToString();
                        if (jar[i]["projectName"] != null && !string.IsNullOrEmpty(jar[i]["projectName"].ToString()))
                            order.projectName = jar[i]["projectName"].ToString();
                        if (jar[i]["productDate"] != null && !string.IsNullOrEmpty(jar[i]["productDate"].ToString()))
                            order.productDate = Convert.ToDateTime(jar[i]["productDate"].ToString());
                        if (jar[i]["constructSite"] != null && !string.IsNullOrEmpty(jar[i]["constructSite"].ToString().Trim()))
                            order.constructSite = jar[i]["constructSite"].ToString().Trim();
                        if (jar[i]["slump"] != null && !string.IsNullOrEmpty(jar[i]["slump"].ToString().Trim()))
                            order.slump = jar[i]["slump"].ToString().Trim();
                        if (jar[i]["otherRequirement"] != null && !string.IsNullOrEmpty(jar[i]["otherRequirement"].ToString()))
                            order.otherRequirement = jar[i]["otherRequirement"].ToString();
                        if (jar[i]["transport"] != null && !string.IsNullOrEmpty(jar[i]["transport"].ToString()))
                            order.transport = jar[i]["transport"].ToString();
                        if (!string.IsNullOrEmpty(jar[i]["basicPath"].ToString()))
                            order.basicPath = decimal.Parse(jar[i]["basicPath"].ToString());
                        else
                            order.basicPath = 0;
                        if (!string.IsNullOrEmpty(jar[i]["basicPrice"].ToString()))
                            order.basicPrice = decimal.Parse(jar[i]["basicPrice"].ToString());
                        else
                            order.basicPrice = 0;
                        if (!string.IsNullOrEmpty(jar[i]["superPrice"].ToString()))
                            order.superPrice = decimal.Parse(jar[i]["superPrice"].ToString());
                        else
                            order.superPrice = 0;
                        if (!string.IsNullOrEmpty(jar[i]["areaPrice"].ToString()))
                            order.areaPrice = decimal.Parse(jar[i]["areaPrice"].ToString());
                        else
                            order.areaPrice = 0;
                        if (jar[i]["strengthGrade"] != null && !string.IsNullOrEmpty(jar[i]["strengthGrade"].ToString()))
                            order.strengthGrade = jar[i]["strengthGrade"].ToString();
                        if (jar[i]["workingCenterId"] != null && !string.IsNullOrEmpty(jar[i]["workingCenterId"].ToString()))
                            order.workingId = long.Parse(jar[i]["workingCenterId"].ToString());
                        if (jar[i]["workingCenterName"] != null && !string.IsNullOrEmpty(jar[i]["workingCenterName"].ToString()))
                            order.workingCenterName = jar[i]["workingCenterName"].ToString();
                        if (jar[i]["areaName"] != null && !string.IsNullOrEmpty(jar[i]["areaName"].ToString()))
                            order.areaName = jar[i]["areaName"].ToString();
                        if (jar[i]["actualRange"] != null && !string.IsNullOrEmpty(jar[i]["actualRange"].ToString()))
                            order.actualRange = decimal.Parse(jar[i]["actualRange"].ToString());
                        else
                            order.actualRange = 0;
                        if (jar[i]["executeStatus"] != null && !string.IsNullOrEmpty(jar[i]["executeStatus"].ToString()))
                            order.executeStatus = int.Parse(jar[i]["executeStatus"].ToString());
                        else
                            order.executeStatus = 0;
                        if (jar[i]["audited"] != null && !string.IsNullOrEmpty(jar[i]["audited"].ToString()))
                            order.audited = Convert.ToBoolean((jar[i]["audited"].ToString()));
                        if (jar[i]["forExecuteStatus"] != null && !string.IsNullOrEmpty(jar[i]["forExecuteStatus"].ToString()))
                            order.isHaveNewFormula = Convert.ToBoolean((jar[i]["forExecuteStatus"].ToString()));
                        else
                            order.isHaveNewFormula = false;
                        order.unitPrice = order.productPrice + order.areaPrice;
                        order.version = long.Parse(jar[i]["version"].ToString());
                        //施工地址
                        if (jar[i]["freeField15"] != null && !string.IsNullOrEmpty(jar[i]["freeField15"].ToString().Trim()))
                            order.constructionAddr = jar[i]["freeField15"].ToString().Trim();
                        saleOrdersList.Add(order);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("获取订单异常：" + ex.ToString() + ",返回JSon数据：" + jsondata);
                throw new Exception(ex.Message);
            }
            return saleOrdersList;
        }

        //获取订单数量
        public static int GetSaleOrdersSizes(string user, string password, int belongcorpId, string orderCode, long customerId, long materialId,
            DateTime start, DateTime end, string types, string audites)
        {
            int size = -1;
            try
            {
                string parameterStr = belongcorpId + "," + orderCode + "," + customerId + ",,," + materialId + ",,,," 
                    + types + "," + audites + ","+ start.ToString("yyyy-MM-dd") + "," + end.ToString("yyyy-MM-dd");
                string strErrMsg = string.Empty;
                int res = CommonHelper.getJsonData("saleOrderSizes", parameterStr, ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password,ref name))
                            return -1;
                        return GetSaleOrdersSizes(user, password, belongcorpId, orderCode, customerId, materialId, start, end, types, audites);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
                else//json取值正常
                {
                    string jsondata = CGlobal.strSwapJsonData;
                    CGlobal.strSwapJsonData = string.Empty;
                    if (jsondata == "[]")
                        return -1;
                    size = int.Parse(jsondata);
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("获取订单数量异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return size;
        }

        // 获取客户
        public static List<CustomerInfo> GetCustomers(string user, string password, 
            long customerId, string customerCode, string customerName)
        {
            List<CustomerInfo> customerList = new List<CustomerInfo>();
            try
            {
                string parameterStr = customerId.ToString() + "," + customerCode + "," + customerName;
                string strErrMsg = string.Empty;
                int res = CommonHelper.getJsonData("customerlists", parameterStr, ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password,ref name))
                            return null;
                        GetCustomers(user, password, customerId, customerCode, customerName);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
                else//json取值正常
                {
                    string jsondata = CGlobal.strSwapJsonData;
                    CGlobal.strSwapJsonData = string.Empty;
                    if (jsondata == "[]")
                        return null;
                    JArray jar = JArray.Parse(jsondata);
                    for (int i = 0; i < jar.Count; i++)
                    {
                        CustomerInfo customer = new CustomerInfo();
                        customer.customerId = long.Parse(jar[i]["id"].ToString());
                        customer.customerCode = jar[i]["code"].ToString();
                        customer.customerName = jar[i]["name"].ToString();
                        customerList.Add(customer);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("获取客户异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return customerList;
        }

        // 获取物料
        public static List<MaterialInfo> GetMaterials(string user, string password)
        {
            List<MaterialInfo> materialList = new List<MaterialInfo>();
            try
            {
                string strErrMsg = string.Empty;
                int res = CommonHelper.getJsonData("materials", "", ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string nameStr = "";
                        if (!CommonHelper.JsonLogin(user, password, ref nameStr))
                            return null;
                        GetMaterials(user, password);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
                else//json取值正常
                {
                    string jsondata = CGlobal.strSwapJsonData;
                    CGlobal.strSwapJsonData = string.Empty;
                    if (jsondata == "[]")
                        return null;
                    JArray jar = JArray.Parse(jsondata);
                    for (int i = 0; i < jar.Count; i++)
                    {
                        MaterialInfo material = new MaterialInfo();
                        material.materialId = long.Parse(jar[i]["materialId"].ToString());
                        material.materialCode = jar[i]["materialCode"].ToString();
                        material.materialName = jar[i]["materialName"].ToString();
                        material.materialSstd = jar[i]["materialSstd"].ToString();
                        materialList.Add(material);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("获取物料信息异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return materialList;
        }

        //获取工程名称
        public static List<ProjectInfo> GetProjectInfo(string user, string password, string projectName)
        {
            List<ProjectInfo> projecInfoList = new List<ProjectInfo>();
            string strErrMsg = string.Empty;
            try
            {
                int res = CommonHelper.getJsonData("projectName", projectName, ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password,ref name))
                            return null;
                        GetProjectInfo(user, password, projectName);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
                else//json取值正常
                {
                    string jsondata = CGlobal.strSwapJsonData;
                    CGlobal.strSwapJsonData = string.Empty;
                    if (jsondata == "[]")
                        return null;
                    JArray jar = JArray.Parse(jsondata);
                    for (int i = 0; i < jar.Count; i++)
                    {
                        ProjectInfo info = new ProjectInfo();
                        if (!string.IsNullOrEmpty(jar[i]["name"].ToString()))
                            info.name = jar[i]["name"].ToString();
                        if (!string.IsNullOrEmpty(jar[i]["range"].ToString()))
                            info.range = jar[i]["range"].ToString();
                        ZoneInfo zone = new ZoneInfo();
                        if (!string.IsNullOrEmpty(jar[i]["areaId"].ToString()))
                            zone.id = long.Parse(jar[i]["areaId"].ToString());
                        if (!string.IsNullOrEmpty(jar[i]["names"].ToString()))
                            zone.names = jar[i]["names"].ToString();
                        if (!string.IsNullOrEmpty(jar[i]["basicPrice"].ToString()))
                            zone.basicPrice = decimal.Parse(jar[i]["basicPrice"].ToString());
                        if (!string.IsNullOrEmpty(jar[i]["price"].ToString()))
                            zone.price = decimal.Parse(jar[i]["price"].ToString());
                        if (!string.IsNullOrEmpty(jar[i]["overPrice"].ToString()))
                            zone.overPrice = decimal.Parse(jar[i]["overPrice"].ToString());
                        if (!string.IsNullOrEmpty(jar[i]["supplyPrice"].ToString()))
                            zone.supplyPrice = decimal.Parse(jar[i]["supplyPrice"].ToString());
                        if (!string.IsNullOrEmpty(jar[i]["basicDistance"].ToString()))
                            zone.basicDistance = decimal.Parse(jar[i]["basicDistance"].ToString());
                        if (!string.IsNullOrEmpty(jar[i]["belongCorp"].ToString()))
                            zone.belongCorp = jar[i]["belongCorp"].ToString();
                        info.zoneInfo = zone;
                        projecInfoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("根据工程名称" + projectName + "获取工程信息异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return projecInfoList;
        }

        //获取坍落度
        public static List<SlumpInfo> GetSlumpInfo(string user, string password, string slumpName)
        {
            List<SlumpInfo> slumpInfoList = new List<SlumpInfo>();
            string strErrMsg = string.Empty;
            try
            {
                int res = CommonHelper.getJsonData("slump", slumpName, ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password,ref name))
                            return null;
                        GetSlumpInfo(user, password, slumpName);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
                else//json取值正常
                {
                    string jsondata = CGlobal.strSwapJsonData;
                    CGlobal.strSwapJsonData = string.Empty;
                    if (jsondata == "[]")
                        return null;
                    JArray jar = JArray.Parse(jsondata);
                    for (int i = 0; i < jar.Count; i++)
                    {
                        SlumpInfo info = new SlumpInfo();
                        if (!string.IsNullOrEmpty(jar[i]["id"].ToString()))
                            info.id = long.Parse(jar[i]["id"].ToString());
                        if (!string.IsNullOrEmpty(jar[i]["name"].ToString()))
                            info.name = jar[i]["name"].ToString();
                        slumpInfoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("根据坍落度名称" + slumpName + "获取坍落度信息异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return slumpInfoList;
        }

        //获取区域
        public static List<ZoneInfo> GetZoneInfo(string user, string password, int belongcorpId)
        {
            List<ZoneInfo> zoneInfoList = new List<ZoneInfo>();
            string strErrMsg = string.Empty;
            try
            {
                int res = CommonHelper.getJsonData("areas", belongcorpId.ToString(), ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password,ref name))
                            return null;
                        GetZoneInfo(user, password, belongcorpId);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
                else//json取值正常
                {
                    string jsondata = CGlobal.strSwapJsonData;
                    CGlobal.strSwapJsonData = string.Empty;
                    if (jsondata == "[]")
                        return null;
                    JArray jar = JArray.Parse(jsondata);
                    for (int i = 0; i < jar.Count; i++)
                    {
                        ZoneInfo info = new ZoneInfo();
                        if (!string.IsNullOrEmpty(jar[i]["id"].ToString()))
                            info.id = long.Parse(jar[i]["id"].ToString());
                        if (!string.IsNullOrEmpty(jar[i]["names"].ToString()))
                            info.names = jar[i]["names"].ToString();
                        if (!string.IsNullOrEmpty(jar[i]["basicPrice"].ToString()))
                            info.basicPrice = decimal.Parse(jar[i]["basicPrice"].ToString());
                        if (!string.IsNullOrEmpty(jar[i]["price"].ToString()))
                            info.price = decimal.Parse(jar[i]["price"].ToString());
                        if (!string.IsNullOrEmpty(jar[i]["overPrice"].ToString()))
                            info.overPrice = decimal.Parse(jar[i]["overPrice"].ToString());
                        if (!string.IsNullOrEmpty(jar[i]["supplyPrice"].ToString()))
                            info.supplyPrice = decimal.Parse(jar[i]["supplyPrice"].ToString());
                        if (!string.IsNullOrEmpty(jar[i]["basicDistance"].ToString()))
                            info.basicDistance = decimal.Parse(jar[i]["basicDistance"].ToString());
                        if (!string.IsNullOrEmpty(jar[i]["belongCorp"].ToString()))
                            info.belongCorp = jar[i]["belongCorp"].ToString();
                        zoneInfoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("根据所属公司Id" + belongcorpId + "获取区域信息异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return zoneInfoList;
        }

        //获取施工部位
        public static List<ConstructSiteInfo> GetConstructSiteInfo(string user, string password)
        {
            List<ConstructSiteInfo> constructSiteInfoList = new List<ConstructSiteInfo>();
            string strErrMsg = string.Empty;
            try
            {
                int res = CommonHelper.getJsonData("projectPosition", "", ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password,ref name))
                            return null;
                        GetConstructSiteInfo(user, password);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
                else//json取值正常
                {
                    string jsondata = CGlobal.strSwapJsonData;
                    CGlobal.strSwapJsonData = string.Empty;
                    if (jsondata == "[]")
                        return null;
                    JArray jar = JArray.Parse(jsondata);
                    for (int i = 0; i < jar.Count; i++)
                    {
                        ConstructSiteInfo info = new ConstructSiteInfo();
                        if (!string.IsNullOrEmpty(jar[i]["id"].ToString()))
                            info.id = long.Parse(jar[i]["id"].ToString());
                        if (!string.IsNullOrEmpty(jar[i]["names"].ToString()))
                            info.names = jar[i]["names"].ToString();
                        constructSiteInfoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("获取施工部位信息异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return constructSiteInfoList;
        }

        //获取输送方式
        public static List<TransportInfo> GetTransportInfo(string user, string password)
        {
            List<TransportInfo> transportInfoList = new List<TransportInfo>();
            string strErrMsg = string.Empty;
            try
            {
                int res = CommonHelper.getJsonData("transport", "", ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password,ref name))
                            return null;
                        GetConstructSiteInfo(user, password);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
                else//json取值正常
                {
                    string jsondata = CGlobal.strSwapJsonData;
                    CGlobal.strSwapJsonData = string.Empty;
                    if (jsondata == "[]")
                        return null;
                    JArray jar = JArray.Parse(jsondata);
                    for (int i = 0; i < jar.Count; i++)
                    {
                        TransportInfo info = new TransportInfo();
                        if (!string.IsNullOrEmpty(jar[i]["id"].ToString()))
                            info.id = long.Parse(jar[i]["id"].ToString());
                        if (!string.IsNullOrEmpty(jar[i]["names"].ToString()))
                            info.names = jar[i]["names"].ToString();
                        if (!string.IsNullOrEmpty(jar[i]["types"].ToString()))
                            info.types = long.Parse(jar[i]["types"].ToString());
                        transportInfoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("获取输送方式信息异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return transportInfoList;
        }

        //保存销售订单
        public static bool SaveSaleOrder(string user, string password)
        {
            string strErrMsg = string.Empty;
            try
            {
                int res = CommonHelper.putJsonData("saveSaleOrder", "", ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password,ref name))
                            return false;
                        return SaveSaleOrder(user, password);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("保存销售订单信息异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return true;
        }

        //审核订单
        public static bool AuditedSaleOrder(string user, string password, long mainId, bool audited)
        {
            string strErrMsg = string.Empty;
            try
            {
                string parameter = mainId.ToString() + "," + audited.ToString();
                int res = CommonHelper.putJsonData("audited", parameter, ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password,ref name))
                            return false;
                        return SaveSaleOrder(user, password);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("审核订单信息异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return true;
        }

        //获取实施配比执行状态信息
        public static List<FormulasStatusInfo> GetFormulasStatusInfoList(string user, string password)
        {
            List<FormulasStatusInfo> list = new List<FormulasStatusInfo>();
            try
            {
                string strErrMsg = string.Empty;
                int res = CommonHelper.getJsonData("getSaleFormulasByStatus", "false", ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password,ref name))
                            return null;
                        GetFormulasStatusInfoList(user, password);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
                else//json取值正常
                {
                    string jsondata = CGlobal.strSwapJsonData;
                    CGlobal.strSwapJsonData = string.Empty;
                    if (jsondata == "[]")
                        return null;
                    JArray jar = JArray.Parse(jsondata);
                    for (int i = 0; i < jar.Count; i++)
                    {
                        FormulasStatusInfo info = new FormulasStatusInfo();
                        info.id = long.Parse(jar[i]["id"].ToString());
                        info.saleId = long.Parse(jar[i]["saleId"].ToString());
                        info.orderCode = jar[i]["orderCode"].ToString();
                        info.formulaDate = Convert.ToDateTime(jar[i]["formulaDate"].ToString());
                        if (jar[i]["executeDate"] != null && !string.IsNullOrEmpty(jar[i]["executeDate"].ToString()))
                            info.executeDate = Convert.ToDateTime(jar[i]["executeDate"].ToString());
                        info.executeStatus = Convert.ToBoolean(jar[i]["executeStatus"].ToString());
                        list.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("获取实施配比执行状态异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return list;
        }

        //获取关联客户销售金额type= 0,销售订单 1发货订单
        public static decimal GetCusBalances(string user, string password, long belongCorpId, long customerId, int type)
        {
            decimal cusBalances = 0;
            string strErrMsg = string.Empty;
            try
            {
                string parameterStr = belongCorpId.ToString() + "," + customerId.ToString() + "," + type.ToString();
                int res = CommonHelper.getJsonData("cusBalances", parameterStr, ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password,ref name))
                            return -1;
                        return GetCusBalances(user, password, belongCorpId, customerId, type);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
                else//json取值正常
                {
                    string jsondata = CGlobal.strSwapJsonData;
                    CGlobal.strSwapJsonData = string.Empty;
                    if (jsondata == "[]")
                        return -1;
                    cusBalances = decimal.Parse(jsondata);
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("获取关联客户销售金额异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return cusBalances;
        }

        //更新订单执行状态
        public static bool UpdateExecuteStatus(string user, string password, long orderId, int type)
        {
            string strErrMsg = string.Empty;
            try
            {
                string parameterStr = orderId.ToString() + ","+ type.ToString();
                int res = CommonHelper.putJsonData("updateExecuteStatus", parameterStr, ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password,ref name))
                            return false;
                        return UpdateExecuteStatus(user, password, orderId, type);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("更新订单执行状态异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return true;
        }

        //删除订单
        public static bool DelSaleLine(string user, string password, long orderId)
        {
            string strErrMsg = string.Empty;
            try
            {
                int res = CommonHelper.putJsonData("delSaleLine", orderId.ToString(), ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password,ref name))
                            return false;
                        return DelSaleLine(user, password, orderId);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("删除订单异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return true;
        }


        //更新实施配比执行状态
        public static bool UpdateFormulaExecuteStatus(string user, string password, long formulaId, bool type)
        {
            string strErrMsg = string.Empty;
            try
            {
                string parameterStr = formulaId.ToString() + "," + type.ToString();
                int res = CommonHelper.putJsonData("updateFormulaExecuteStatus", parameterStr, ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password,ref name))
                            return false;
                        return UpdateFormulaExecuteStatus(user, password, formulaId, type);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("更新实施配比执行状态异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return true;
        }

        //退砼保存
        public static bool SaveReturnSale(string user, string password, BackConcreteInfo backConcreteInfo)
        {
            string strErrMsg = string.Empty;
            try
            {
                string parameterStr = backConcreteInfo.DeliveryId.ToString() + "," + backConcreteInfo.State.ToString() + ","
                    + backConcreteInfo.Number.ToString() + "," + backConcreteInfo.Explain + "," + backConcreteInfo.OrderId.ToString();
                int res = CommonHelper.putJsonData("saveReturnSale", parameterStr, ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password,ref name))
                            return false;
                        return SaveReturnSale(user, password, backConcreteInfo);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("退砼保存异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return true;
        }

        //发货单红冲
        public static bool SaveBackSale(string user, string password, long deliveryId)
        {
            string strErrMsg = string.Empty;
            try
            {
                int res = CommonHelper.putJsonData("saveBackSale", deliveryId.ToString(), ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password,ref name))
                            return false;
                        return SaveBackSale(user, password, deliveryId);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("发货单红冲异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return true;
        }

        //根据发货单获取发货明细
        public static List<DeliveryInfo> GetSaleDeliverys(string user, string password, long orderId)
        {
            List<DeliveryInfo> deliveryInfoList = new List<DeliveryInfo>();
            try
            {
                string strErrMsg = string.Empty;
                string parameter = orderId.ToString();

                string parameterStr = orderId + ",0," + "" + "," + -1 + ",,," + -1 + ",,,," + DateTime.Now.ToString("yyyy-MM-dd")
                    + "," + DateTime.Now.ToString("yyyy-MM-dd") + "," + 1 + "," + 50;

                int res = CommonHelper.getJsonData("saleDeliverys", parameterStr, ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password,ref name))
                            return null;
                        return GetSaleDeliverys(user, password, orderId);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
                else//json取值正常
                {
                    string jsondata = CGlobal.strSwapJsonData;
                    CGlobal.strSwapJsonData = string.Empty;
                    if (jsondata == "[]")
                        return null;
                    JArray jar = JArray.Parse(jsondata);
                    for (int i = 0; i < jar.Count; i++)
                    {
                        DeliveryInfo delivery = new DeliveryInfo();
                        delivery.id = long.Parse(jar[i]["id"].ToString());
                        delivery.deliDate = Convert.ToDateTime(jar[i]["deliDate"].ToString());
                        delivery.deliCode = jar[i]["deliCode"].ToString();
                        delivery.customerId = long.Parse(jar[i]["customerId"].ToString());
                        delivery.customerCode = jar[i]["customerCode"].ToString();
                        delivery.customerName = jar[i]["customerName"].ToString();
                        delivery.materialId = long.Parse(jar[i]["materialId"].ToString());
                        delivery.materialCode = jar[i]["materialCode"].ToString();
                        delivery.materialName = jar[i]["materialName"].ToString();
                        delivery.materialSstd = jar[i]["materialSstd"].ToString();
                        if (!string.IsNullOrEmpty(jar[i]["deliQuantity"].ToString()))
                            delivery.deliQuantity = decimal.Parse(jar[i]["deliQuantity"].ToString());
                        else
                            delivery.deliQuantity = 0;
                        if (!string.IsNullOrEmpty(jar[i]["outQuantity"].ToString()))
                            delivery.outQuantity = decimal.Parse(jar[i]["outQuantity"].ToString());
                        else
                            delivery.outQuantity = 0;
                        if (!string.IsNullOrEmpty(jar[i]["recoveredQuantity"].ToString()))
                            delivery.recoveredQuantity = decimal.Parse(jar[i]["recoveredQuantity"].ToString());
                        else
                            delivery.recoveredQuantity = 0;
                        if (!string.IsNullOrEmpty(jar[i]["productQuantity"].ToString()))
                            delivery.productQuantity = decimal.Parse(jar[i]["productQuantity"].ToString());
                        else
                            delivery.productQuantity = 0;
                        if (jar[i]["workingCenterName"] != null && !string.IsNullOrEmpty(jar[i]["workingCenterName"].ToString()))
                            delivery.workingCenterName = jar[i]["workingCenterName"].ToString();
                        if (jar[i]["carCode"] != null && !string.IsNullOrEmpty(jar[i]["carCode"].ToString()))
                            delivery.carCode = jar[i]["carCode"].ToString();
                        if (jar[i]["driverName"] != null && !string.IsNullOrEmpty(jar[i]["driverName"].ToString()))
                            delivery.driverName = jar[i]["driverName"].ToString();
                        if (jar[i]["makerName"] != null && !string.IsNullOrEmpty(jar[i]["makerName"].ToString()))
                            delivery.makerName = jar[i]["makerName"].ToString();
                        if (jar[i]["remarks"] != null && !string.IsNullOrEmpty(jar[i]["remarks"].ToString()))
                            delivery.remarks = jar[i]["remarks"].ToString();
                        if (jar[i]["returnStatus"] != null && !string.IsNullOrEmpty(jar[i]["returnStatus"].ToString()))
                            delivery.returnStatus = int.Parse(jar[i]["returnStatus"].ToString());
                        else
                            delivery.returnStatus = -1;
                        if (!string.IsNullOrEmpty(jar[i]["returnQuantity"].ToString()))
                            delivery.returnQuantity = decimal.Parse(jar[i]["returnQuantity"].ToString());
                        else
                            delivery.returnQuantity = 0;
                        if (jar[i]["invalid"] != null && !string.IsNullOrEmpty(jar[i]["invalid"].ToString()))
                            delivery.invalid = Convert.ToBoolean((jar[i]["invalid"].ToString()));
                        deliveryInfoList.Add(delivery);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("根据订单ID：" + orderId + "获取发货单异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return deliveryInfoList;
        }

        //保存销售订单
        public static bool SaveSaleDelivery(string user, string password)
        {
            string strErrMsg = string.Empty;
            try
            {
                int res = CommonHelper.putJsonData("saveSaleDelivery", "", ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password,ref name))
                            return false;
                        return SaveSaleDelivery(user, password);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("保存发货单异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return true;
        }

        //查询费用类别
        public static List<ExpenseTypeData> GetExpenseType(string user, string password)
        {
            List<ExpenseTypeData> expenseTypeDataList = new List<ExpenseTypeData>();
            string strErrMsg = string.Empty;
            try
            {
                int res = CommonHelper.getJsonData("expenseType", "", ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password,ref name))
                            return null;
                        GetExpenseType(user, password);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
                else//json取值正常
                {
                    string jsondata = CGlobal.strSwapJsonData;
                    CGlobal.strSwapJsonData = string.Empty;
                    if (jsondata == "[]")
                        return null;
                    JArray jar = JArray.Parse(jsondata);
                    for (int i = 0; i < jar.Count; i++)
                    {
                        ExpenseTypeData info = new ExpenseTypeData();
                        if (!string.IsNullOrEmpty(jar[i]["id"].ToString()))
                            info.id = long.Parse(jar[i]["id"].ToString());
                        if (!string.IsNullOrEmpty(jar[i]["names"].ToString()))
                            info.names = jar[i]["names"].ToString();
                        if (!string.IsNullOrEmpty(jar[i]["types"].ToString()))
                            info.types = jar[i]["types"].ToString();
                        expenseTypeDataList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("查询费用类别信息异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return expenseTypeDataList;
        }

        //查询销售费用,id为销售出库单ID
        public static List<SaleExpenseData> GetSaleExpense(string user, string password, long saleOutId, long saleOrderId, int type)
        {
            List<SaleExpenseData> saleExpenseDataList = new List<SaleExpenseData>();
            try
            {
                string strErrMsg = string.Empty;
                int res = -1;
                if (type == (int)ExpenseTypeEnum.ExpenseAll)
                {
                    res = CommonHelper.getJsonData("getSaleExpenseAll", saleOrderId.ToString(), ref strErrMsg);
                }
                else if (type == (int)ExpenseTypeEnum.ExpenseSingle)
                {
                    if (saleOutId == -1)
                        return null;
                    res = CommonHelper.getJsonData("getSaleExpenseByOutLineId", saleOutId.ToString(), ref strErrMsg);
                }
                else if (type == (int)ExpenseTypeEnum.ExpenseEntire)
                {
                    res = CommonHelper.getJsonData("getSaleExpenseBySaleLineId", saleOrderId.ToString(), ref strErrMsg);
                }
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password,ref name))
                            return null;
                        return GetSaleExpense(user, password, saleOutId, saleOrderId, type);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
                else//json取值正常
                {
                    string jsondata = CGlobal.strSwapJsonData;
                    CGlobal.strSwapJsonData = string.Empty;
                    if (jsondata == "[]")
                        return null;
                    JArray jar = JArray.Parse(jsondata);
                    for (int i = 0; i < jar.Count; i++)
                    {
                        SaleExpenseData saleExpense = new SaleExpenseData();
                        saleExpense.id = long.Parse(jar[i]["id"].ToString());
                        if (jar[i]["expenseDate"] != null && !string.IsNullOrEmpty(jar[i]["expenseDate"].ToString()))
                            saleExpense.expenseDate = Convert.ToDateTime((jar[i]["expenseDate"].ToString()));
                        if (jar[i]["expenseName"] != null && !string.IsNullOrEmpty(jar[i]["expenseName"].ToString()))
                            saleExpense.expenseName = jar[i]["expenseName"].ToString();
                        if (jar[i]["quantity"] != null && !string.IsNullOrEmpty(jar[i]["quantity"].ToString()))
                            saleExpense.quantity = decimal.Parse(jar[i]["quantity"].ToString());
                        else
                            saleExpense.quantity = 0;
                        if (jar[i]["price"] != null && !string.IsNullOrEmpty(jar[i]["price"].ToString()))
                            saleExpense.price = decimal.Parse(jar[i]["price"].ToString());
                        else
                            saleExpense.price = 0;
                        if (jar[i]["moneys"] != null && !string.IsNullOrEmpty(jar[i]["moneys"].ToString()))
                            saleExpense.moneys = decimal.Parse(jar[i]["moneys"].ToString());
                        else
                            saleExpense.moneys = 0;
                        if (jar[i]["remarks"] != null && !string.IsNullOrEmpty(jar[i]["remarks"].ToString()))
                            saleExpense.remarks = jar[i]["remarks"].ToString();
                        if (jar[i]["status"] != null && !string.IsNullOrEmpty(jar[i]["status"].ToString()))
                            saleExpense.status = int.Parse((jar[i]["status"].ToString()));
                        if (jar[i]["checker"] != null && !string.IsNullOrEmpty(jar[i]["checker"].ToString()))
                            saleExpense.checker = jar[i]["checker"].ToString();
                        if (jar[i]["checkDate"] != null && !string.IsNullOrEmpty(jar[i]["checkDate"].ToString()))
                            saleExpense.checkDate = Convert.ToDateTime((jar[i]["checkDate"].ToString()));

                        if (jar[i]["driverName"] != null && !string.IsNullOrEmpty(jar[i]["driverName"].ToString()))
                            saleExpense.driverName = jar[i]["driverName"].ToString();
                        if (jar[i]["carCode"] != null && !string.IsNullOrEmpty(jar[i]["carCode"].ToString()))
                            saleExpense.carCode = jar[i]["carCode"].ToString();
                        if (jar[i]["deliveryDate"] != null && !string.IsNullOrEmpty(jar[i]["deliveryDate"].ToString()))
                            saleExpense.deliveryDate = Convert.ToDateTime((jar[i]["deliveryDate"].ToString()));
                        if (jar[i]["isAuto"] != null && !string.IsNullOrEmpty(jar[i]["isAuto"].ToString()))
                            saleExpense.isAuto = Convert.ToBoolean((jar[i]["isAuto"].ToString()));
                        if (jar[i]["invalid"] != null && !string.IsNullOrEmpty(jar[i]["invalid"].ToString()))
                            saleExpense.invalid = Convert.ToBoolean((jar[i]["invalid"].ToString()));
                        if (jar[i]["sheetMaker"] != null && !string.IsNullOrEmpty(jar[i]["sheetMaker"].ToString()))
                            saleExpense.sheetMaker = jar[i]["sheetMaker"].ToString();
                        if (jar[i]["createDate"] != null && !string.IsNullOrEmpty(jar[i]["createDate"].ToString()))
                            saleExpense.createDate = Convert.ToDateTime((jar[i]["createDate"].ToString()));
                        saleExpenseDataList.Add(saleExpense);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("获取费用信息异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return saleExpenseDataList;
        }

        //销售出库 
        public static List<SaleOutsData> GetSaleOutsByOrder(string user, string password, long orderId)
        {
            List<SaleOutsData> saleOutsDataList = new List<SaleOutsData>();
            try
            {
                string strErrMsg = string.Empty;
                int res = CommonHelper.getJsonData("getSaleOutsByOrder", orderId.ToString(), ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password, ref name))
                            return null;
                        return GetSaleOutsByOrder(user, password, orderId);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
                else//json取值正常
                {
                    string jsondata = CGlobal.strSwapJsonData;
                    CGlobal.strSwapJsonData = string.Empty;
                    if (jsondata == "[]")
                        return null;
                    JArray jar = JArray.Parse(jsondata);
                    for (int i = 0; i < jar.Count; i++)
                    {
                        SaleOutsData saleOutsData = new SaleOutsData();
                        saleOutsData.id = long.Parse(jar[i]["id"].ToString());
                        if (jar[i]["customerCode"] != null && !string.IsNullOrEmpty(jar[i]["customerCode"].ToString()))
                            saleOutsData.customerCode = jar[i]["customerCode"].ToString();
                        if (jar[i]["customerName"] != null && !string.IsNullOrEmpty(jar[i]["customerName"].ToString()))
                            saleOutsData.customerName = jar[i]["customerName"].ToString();

                        if (jar[i]["outDate"] != null && !string.IsNullOrEmpty(jar[i]["outDate"].ToString()))
                            saleOutsData.outDate = Convert.ToDateTime((jar[i]["outDate"].ToString()));
                        if (jar[i]["outQuantity"] != null && !string.IsNullOrEmpty(jar[i]["outQuantity"].ToString()))
                            saleOutsData.outQuantity = decimal.Parse(jar[i]["outQuantity"].ToString());
                        else
                            saleOutsData.outQuantity = 0;
                        if (jar[i]["projectName"] != null && !string.IsNullOrEmpty(jar[i]["projectName"].ToString()))
                            saleOutsData.projectName = jar[i]["projectName"].ToString();
                        if (jar[i]["constructSite"] != null && !string.IsNullOrEmpty(jar[i]["constructSite"].ToString()))
                            saleOutsData.constructSite = jar[i]["constructSite"].ToString();
                        if (jar[i]["slump"] != null && !string.IsNullOrEmpty(jar[i]["slump"].ToString()))
                            saleOutsData.slump = jar[i]["slump"].ToString();
                        if (jar[i]["areaName"] != null && !string.IsNullOrEmpty(jar[i]["areaName"].ToString()))
                            saleOutsData.areaName = jar[i]["areaName"].ToString();
                        if (jar[i]["unitPrice"] != null && !string.IsNullOrEmpty(jar[i]["unitPrice"].ToString()))
                            saleOutsData.unitPrice = decimal.Parse(jar[i]["unitPrice"].ToString());
                        else
                            saleOutsData.unitPrice = 0;
                        if (jar[i]["money"] != null && !string.IsNullOrEmpty(jar[i]["money"].ToString()))
                            saleOutsData.money = decimal.Parse(jar[i]["money"].ToString());
                        else
                            saleOutsData.money = 0;
                        if (jar[i]["finalMoney"] != null && !string.IsNullOrEmpty(jar[i]["finalMoney"].ToString()))
                            saleOutsData.finalMoney = decimal.Parse(jar[i]["finalMoney"].ToString());
                        else
                            saleOutsData.finalMoney = 0;
                        if (jar[i]["finalPrice"] != null && !string.IsNullOrEmpty(jar[i]["finalPrice"].ToString()))
                            saleOutsData.finalPrice = decimal.Parse(jar[i]["finalPrice"].ToString());
                        else
                            saleOutsData.finalPrice = 0;
                        if (jar[i]["invoiceMoney"] != null && !string.IsNullOrEmpty(jar[i]["invoiceMoney"].ToString()))
                            saleOutsData.invoiceMoney = decimal.Parse(jar[i]["invoiceMoney"].ToString());
                        else
                            saleOutsData.invoiceMoney = 0;
                        if (jar[i]["invoicePrice"] != null && !string.IsNullOrEmpty(jar[i]["invoicePrice"].ToString()))
                            saleOutsData.invoicePrice = decimal.Parse(jar[i]["invoicePrice"].ToString());
                        else
                            saleOutsData.invoicePrice = 0;
                        if (jar[i]["driverName"] != null && !string.IsNullOrEmpty(jar[i]["driverName"].ToString()))
                            saleOutsData.driverName = jar[i]["driverName"].ToString();
                        if (jar[i]["carCode"] != null && !string.IsNullOrEmpty(jar[i]["carCode"].ToString()))
                            saleOutsData.carCode = jar[i]["carCode"].ToString();
                        if (jar[i]["deliveryDate"] != null && !string.IsNullOrEmpty(jar[i]["deliveryDate"].ToString()))
                            saleOutsData.deliveryDate = Convert.ToDateTime((jar[i]["deliveryDate"].ToString()));
                        saleOutsDataList.Add(saleOutsData);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("根据销售订单Id：" + orderId + "获取出库单信息异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return saleOutsDataList;
        }

        //保存费用登记信息
        public static bool SaveSaleExpenses(string user, string password)
        {
            string strErrMsg = string.Empty;
            try
            {
                int res = CommonHelper.putJsonData("saveSaleExpenses", "", ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password, ref name))
                            return false;
                        return SaveSaleExpenses(user, password);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("保存费用登记信息异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return true;
        }

        //删除费用
        public static bool DeleteSaleExpense(string user, string password, long expenseId)
        {
            string strErrMsg = string.Empty;
            try
            {
                int res = CommonHelper.putJsonData("deleteSaleExpense", expenseId.ToString(), ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password, ref name))
                            return false;
                        return DeleteSaleExpense(user, password, expenseId);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("删除费用" + expenseId + "异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return true;
        }

        //费用审核
        public static bool DealSaleExpense(string user, string password)
        {
            string strErrMsg = string.Empty;
            try
            {
                int res = CommonHelper.putJsonData("dealSaleExpense", "", ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password, ref name))
                            return false;
                        return DealSaleExpense(user, password);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("费用审核异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return true;
        }

        //获取产品价格
        public static decimal GetOrderPrice(string user, string password, long belongCorpId, long customerId, long materialId, DateTime orderDate)
        {
            decimal orderPrice = -1;
            string strErrMsg = string.Empty;
            try
            {
                string parameterStr = belongCorpId.ToString() + "," + customerId.ToString() + "," + materialId.ToString() + "," 
                    + orderDate.ToString("yyyy-MM-dd HH:mm:ss");
                int res = CommonHelper.getJsonData("orderPrice", parameterStr, ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password, ref name))
                            return -1;
                        return GetOrderPrice(user, password, belongCorpId, customerId, materialId, orderDate);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
                else//json取值正常
                {
                    string jsondata = CGlobal.strSwapJsonData;
                    CGlobal.strSwapJsonData = string.Empty;
                    if (jsondata == "[]")
                        return -1;
                    orderPrice = decimal.Parse(jsondata);
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("获取产品价格异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return orderPrice;
        }


        //获取实施配比主表信息
        public static List<SaleFormulasData> GetSaleFormulas(string user, string password, long orderId)
        {
            List<SaleFormulasData> list = new List<SaleFormulasData>();
            try
            {
                string strErrMsg = string.Empty;
                int res = CommonHelper.getJsonData("getSaleFormulas", orderId.ToString(), ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password, ref name))
                            return null;
                        GetSaleFormulas(user, password, orderId);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
                else//json取值正常
                {
                    string jsondata = CGlobal.strSwapJsonData;
                    CGlobal.strSwapJsonData = string.Empty;
                    if (jsondata == "[]")
                        return null;
                    JArray jar = JArray.Parse(jsondata);
                    for (int i = 0; i < jar.Count; i++)
                    {
                        SaleFormulasData info = new SaleFormulasData();
                        info.id = long.Parse(jar[i]["id"].ToString());
                        if (jar[i]["labels"] != null && !string.IsNullOrEmpty(jar[i]["labels"].ToString()))
                            info.labels = jar[i]["labels"].ToString();
                        info.formulaDate = Convert.ToDateTime(jar[i]["formulaDate"].ToString());
                        if (jar[i]["executeDate"] != null && !string.IsNullOrEmpty(jar[i]["executeDate"].ToString()))
                            info.executeDate = Convert.ToDateTime(jar[i]["executeDate"].ToString());
                        info.executeStatus = Convert.ToBoolean(jar[i]["executeStatus"].ToString()) == true ? "已执行" : "未执行";
                        if (jar[i]["otherCode"] != null && !string.IsNullOrEmpty(jar[i]["otherCode"].ToString()))
                            info.otherCode = jar[i]["otherCode"].ToString();
                        list.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("获取实施配比主表信息异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return list;
        }


        //获取实施配比字表信息,id为实施配方主表ID
        public static List<SaConcreteFormulaData> GetSaConcreteFormulaLines(string user, string password, long id)
        {
            List<SaConcreteFormulaData> list = new List<SaConcreteFormulaData>();
            try
            {
                string strErrMsg = string.Empty;
                int res = CommonHelper.getJsonData("getSaConcreteFormulaLines", id.ToString(), ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password, ref name))
                            return null;
                        GetSaConcreteFormulaLines(user, password, id);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
                else//json取值正常
                {
                    string jsondata = CGlobal.strSwapJsonData;
                    CGlobal.strSwapJsonData = string.Empty;
                    if (jsondata == "[]")
                        return null;
                    JArray jar = JArray.Parse(jsondata);
                    for (int i = 0; i < jar.Count; i++)
                    {
                        SaConcreteFormulaData info = new SaConcreteFormulaData();
                        info.id = long.Parse(jar[i]["id"].ToString());
                        if (jar[i]["materialId"] != null && !string.IsNullOrEmpty(jar[i]["materialId"].ToString()))
                            info.materialId = long.Parse(jar[i]["materialId"].ToString());
                        if (jar[i]["materialCode"] != null && !string.IsNullOrEmpty(jar[i]["materialCode"].ToString()))
                            info.materialCode = jar[i]["materialCode"].ToString();
                        if (jar[i]["materialName"] != null && !string.IsNullOrEmpty(jar[i]["materialName"].ToString()))
                            info.materialName = jar[i]["materialName"].ToString();
                        if (jar[i]["materialSstd"] != null && !string.IsNullOrEmpty(jar[i]["materialSstd"].ToString()))
                            info.materialSstd = jar[i]["materialSstd"].ToString();
                        info.quantity = decimal.Parse(jar[i]["quantity"].ToString());
                        if (jar[i]["controlStoreName"] != null && !string.IsNullOrEmpty(jar[i]["controlStoreName"].ToString()))
                            info.controlStoreName = jar[i]["controlStoreName"].ToString();
                        list.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("获取实施配比子表信息异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return list;
        }

        //获取同步用户信息
        public static List<UserData> GetSyncUserData(string user, string password)
        {
            string jsondata = "";
            List<UserData> list = new List<UserData>();
            try
            {
                string strErrMsg = string.Empty;
                int res = CommonHelper.getSyncJsonData("sync", "", ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password,ref name))
                            return null;
                        return GetSyncUserData(user, password);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
                else//json取值正常
                {
                    jsondata = CGlobal.strSwapJsonData;
                    CGlobal.strSwapJsonData = string.Empty;
                    if (jsondata == "[]")
                        return null;
                    JObject jo = (JObject)JsonConvert.DeserializeObject(jsondata);
                    string status = jo["status"].ToString();
                    if (status.ToUpper() != "OK")
                        return null;
                    string data = jo["data"].ToString();
                    JArray jar = JArray.Parse(data);
                    for (int i = 0; i < jar.Count; i++)
                    {
                        UserData info = new UserData();
                        info.id = long.Parse(jar[i]["id"].ToString());
                        info.type = long.Parse(jar[i]["syncType"].ToString());
                        if (info.type != 2)
                        {
                            if (jar[i]["value"] != null)
                            {
                                if (jar[i]["value"]["username"] != null && !string.IsNullOrEmpty(jar[i]["value"]["username"].ToString()))
                                    info.username = jar[i]["value"]["username"].ToString();
                                if (jar[i]["value"]["password"] != null && !string.IsNullOrEmpty(jar[i]["value"]["password"].ToString()))
                                    info.password = jar[i]["value"]["password"].ToString();
                                if (jar[i]["value"]["employee"] != null && !string.IsNullOrEmpty(jar[i]["value"]["employee"].ToString()))
                                    info.employeeId = long.Parse(jar[i]["value"]["employee"].ToString());
                                if (jar[i]["value"]["employeeName"] != null && !string.IsNullOrEmpty(jar[i]["value"]["employeeName"].ToString()))
                                    info.employeeName = jar[i]["value"]["employeeName"].ToString();
                                if (jar[i]["value"]["corp"] != null && !string.IsNullOrEmpty(jar[i]["value"]["corp"].ToString()))
                                    info.corp = jar[i]["value"]["corp"].ToString();
                                if (jar[i]["value"]["deptId"] != null && !string.IsNullOrEmpty(jar[i]["value"]["deptId"].ToString()))
                                    info.deptId = long.Parse(jar[i]["value"]["deptId"].ToString());
                            }
                        }
                        list.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("获取同步用户信息异常：" + ex.ToString() + "json数据：" + jsondata);
                throw new Exception(ex.Message);
            }
            return list;
        }

        //返回同步结果
        public static bool SyncResult(string user, string password)
        {
            string strErrMsg = string.Empty;
            try
            {
                int res = CommonHelper.putSyncJsonData("syncResult", "", ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password,ref name))
                            return false;
                        return SyncResult(user, password);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("返回同步结果异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return true;
        }

        //获取服务器时间
        public static DateTime GetSystemTime(string user, string password)
        {
            DateTime systemTime = DateTime.Now;
            string strErrMsg = string.Empty;
            try
            {
                int res = CommonHelper.getJsonData("getDateTime", "", ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password, ref name))
                            return systemTime;
                        return GetSystemTime(user, password);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
                else//json取值正常
                {
                    string jsondata = CGlobal.strSwapJsonData;
                    CGlobal.strSwapJsonData = string.Empty;
                    if (jsondata == "[]")
                        return systemTime;
                    JObject jobj = JObject.Parse(jsondata);
                    if (jobj["dateTime"] != null)
                        systemTime = DateTime.Parse(jobj["dateTime"].ToString());
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("获取服务器时间异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return systemTime;
        }

        //入库保存
        public static bool ProductInStoreSave(string user, string password)
        {
            string strErrMsg = string.Empty;
            try
            {
                int res = CommonHelper.putJsonData("productInStoreSave", "", ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password, ref name))
                            return false;
                        return ProductInStoreSave(user, password);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("入库保存异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return true;
        }

        //出库保存
        public static bool ProductOutStoreSave(string user, string password)
        {
            string strErrMsg = string.Empty;
            try
            {
                int res = CommonHelper.putJsonData("productOutStoreSave", "", ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password, ref name))
                            return false;
                        return ProductOutStoreSave(user, password);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("消耗出库保存异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return true;
        }

        //发货单修改
        public static bool ChangeDelivery(string user, string password)
        {
            string strErrMsg = string.Empty;
            try
            {
                int res = CommonHelper.putJsonData("changeDelivery", "", ref strErrMsg);
                if (res != -1)//系统异常或者业务数据异常
                {
                    LogHelper.error(strErrMsg);
                    if (res == 2)
                    {
                        string name = "";
                        if (!CommonHelper.JsonLogin(user, password, ref name))
                            return false;
                        return ChangeDelivery(user, password);
                    }
                    throw new Exception(strErrMsg == null ? ("type:" + res + ", message:null") : strErrMsg);
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("发货单修改异常：" + ex.ToString());
                throw new Exception(ex.Message);
            }
            return true;
        }
    }
}
